import { getList } from "../const/subApps";

// 给当前的路由跳转打补丁
export const patchRouter = (globalEvent: any, eventName: any) => {
  return function () {
    const e = new Event(eventName)
    globalEvent.apply(this, arguments)
    window.dispatchEvent(e)
  }
}

export const currentApp = () => {
  const currentUrl = window.location.pathname
  return filterApp('activeRule', currentUrl)
}
// 查找上一个和下一个APP内容
export const findAppByRoute = (router: any) => {
  return filterApp('activeRule', router)
}
// 筛选
export const filterApp = (key: any, value: any) => {
  const currentApp = getList().filter((item: any) => item[key] === value)

  return currentApp && currentApp.length ? currentApp[0] : {}
}

// 子应用是否做了切换
export const isTurnChild = () => {
  const { pathname } = window.location
  let prefix: any = pathname.match(/(\/\w+)/)
  if (prefix) {
    prefix = prefix[0]
  }

  window.__ORIGIN_APP__ = window.__CURRENT_SUB_APP__;
  if (window.__CURRENT_SUB_APP__ === window.location.pathname
  ) {
    return false;
  }

  const currentSubApp = window.location.pathname

  if (!currentSubApp) {
    return false
  }
  // 当前路由以改变，修改当前路由
  window.__CURRENT_SUB_APP__ = currentSubApp;


  return true;
}
// export const isTurnChild = () => {
//   const { pathname, hash } = window.location
//   const url = pathname + hash

//   // 当前路由无改变。
//   const currentPrefix = url.match(/(\/\w+)/g)

//   if (
//     currentPrefix &&
//     (currentPrefix[0] === window.__CURRENT_SUB_APP__) &&
//     hash === window.__CURRENT_HASH__
//   ) {
//     return false;
//   }

//   window.__ORIGIN_APP__ = window.__CURRENT_SUB_APP__;

//   const currentSubApp = window.location.pathname

//   if (!currentSubApp) {
//     return false
//   }
//   // 当前路由以改变，修改当前路由
//   window.__CURRENT_SUB_APP__ = currentSubApp;

//   // 判断当前hash值是否改变
//   window.__CURRENT_HASH__ = hash

//   return true;
// }
